classdef ParseMooringsData_dev < ParseTabSepData_dev
  % Parser for (tab-separated) moorings data
  % To use w/ data delimited some other way, first convert file to tab-separated
  % Derived from ParseTabSepData_dev
  % C-tor sig: parser = ParseMooringsData_dev(dataFilename); arg optional
  %     dataFilename: path of the file containing the data to be parsed;
  %                   may be empty
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: N/A
    
    properties
      % Inherited from ParseData_dev
      % dataFilename
        parsedData = struct%;('fields', {}, 'values', {});
    end
    
    methods
        function parser = ParseMooringsData_dev(dataFilename) % C-tor
            if nargin==0
                dataFilename = '';
            end
            parser = parser@ParseTabSepData_dev(dataFilename);
        end
        
        function result = readDataFile(obj)
            try
                result = {};
                [fid, msg] = fopen(obj.dataFilename, 'rt');
                if ~isempty(msg)
                    result = [obj.dataFilename ': ' msg];
                else
                    line = fgetl(fid);
                    fields = {};
                    [fields{1}, rol] = ParseTabSepData_dev.read2tab(line);
                    fspec = '%s ';
                  % Get the column headings and create the format string
                    while ~isempty(rol)
                        [fields{end+1}, rol] = ParseTabSepData_dev.read2tab(rol);
                        if length(fields) > 3
                            fspec = [fspec '%f '];
                        else
                            fspec = [fspec '%s '];
                        end
                    end
                    obj.parsedData.('fields') = fields;
                  % Use textscan to get the data
                    block = 50000;
                    obj.parsedData.('values') = textscan(fid, fspec, block, 'delimiter', char(9));                        
                end
            catch me
                result = [obj.dataFilename ': ' getReport(me)];            
            end
            fclose(fid);
        end
        
    end
    
end

